/*
 * ClientGui.java
 *
 * Created on 30 mai 2007, 10:16
 */

package bibliotheque.client.gui;

import bibliotheque.client.Client;
import bibliotheque.client.Tools;
import bibliotheque.events.BibliothequeQuitListener;
import bibliotheque.events.BibliothequeQuitOccuredEvent;
import bibliotheque.events.ConnexionOccuredListener;
import bibliotheque.events.ConnexionOccuredEvent;
import bibliotheque.events.ConnexionQuitListener;
import bibliotheque.events.ConnexionQuitOccuredEvent;
import java.rmi.RemoteException;

/**
 *
 * @author  guillaume
 */
public class ClientGui extends javax.swing.JFrame implements ConnexionOccuredListener,BibliothequeQuitListener,ConnexionQuitListener {
    
    private ConnexionGui connexion;
    private ToolsGui toolsGui;
    private BibliothequeGui biblio;
    private boolean toolsSet = false;
    
    public void ConnexionOccured(ConnexionOccuredEvent event) {
        try {
            Client client = new Client(event.GetServiceName(),event.GetIp());
            client.InitiateConnexion(event.GetServiceName(),event.GetAdresseRmi(),event.GetPortRmi());
            this.biblio = new BibliothequeGui(client);
        } catch (RemoteException ex) {
            ex.printStackTrace();
        }
        try {
            if ( ! this.toolsSet) {
                Tools tools = new Tools(event.GetServiceName(),event.GetIp());
                tools.InitiateConnection(event.GetAdresseRmi(),event.GetPortRmi());
                this.toolsGui = new ToolsGui(tools);
                this.jTabbedPane1.add(this.toolsGui);
                this.jTabbedPane1.setTitleAt(this.jTabbedPane1.getTabCount() - 1, "Tools Box : " + event.GetServiceName());
                this.toolsSet = true;
            }
            
        } catch (RemoteException ex) {
            ex.printStackTrace();
        }
        //bibli.setLocation(this.jTabbedPane1.get)
        this.biblio.AddBibliothequeQuitListener(this);
        this.jTabbedPane1.add(this.biblio);
        this.jTabbedPane1.setTitleAt(this.jTabbedPane1.getTabCount() - 1, event.GetServiceName());
        this.jTabbedPane1.getSelectedComponent().setEnabled(false);
        this.jTabbedPane1.setSelectedIndex(this.jTabbedPane1.getTabCount() - 1);
        
    }
    
    public void BibliothequeQuitOccured(BibliothequeQuitOccuredEvent event) {
        this.biblio.RemoveBibliothequeQuitListener(this);
        this.jTabbedPane1.remove(this.jTabbedPane1.getSelectedIndex());
    }
    
    public void ConnexionQuitOccured(ConnexionQuitOccuredEvent event) {
        this.connexion.RemoveConnexionListener(this);
        this.connexion.RemoveConnexionQuitListener(this);
        this.dispose();
    }
    
    
    
    /** Creates new form ClientGui */
    public ClientGui() {
        initComponents();
        this.setSize(1280,780);
        this.connexion = new ConnexionGui();
        //this.connexion.setBounds(200,20,200,200);
        this.jTabbedPane1.add(this.connexion);
        this.jTabbedPane1.setTitleAt(0, "Connexion ...");
        //this.jTabbedPane1.add(this.toolsGui);
        //this.jTabbedPane1.setTitleAt(1, "Tools ...");
        this.connexion.AddConnexionListener(this);
        this.connexion.AddConnexionQuitListener(this);
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
    private void initComponents() {
        jTabbedPane1 = new javax.swing.JTabbedPane();
        jMenuBar1 = new javax.swing.JMenuBar();
        jMenu1 = new javax.swing.JMenu();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jMenu1.setText("Menu");
        jMenuBar1.add(jMenu1);

        setJMenuBar(jMenuBar1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jTabbedPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 1208, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jTabbedPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 517, Short.MAX_VALUE)
                .addContainerGap())
        );
        pack();
    }// </editor-fold>//GEN-END:initComponents
    
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new ClientGui().setVisible(true);
            }
        });
    }
    
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JMenu jMenu1;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JTabbedPane jTabbedPane1;
    // End of variables declaration//GEN-END:variables
    
}
